package com.execjar;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ArrayUtil;
import com.execjar.fun.FunctionParse;
import com.execjar.fun.FunctionPound;
import com.execjar.utils.TcpToolUtils;
import com.google.common.collect.EvictingQueue;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Slf4j(topic = "p")
public class AppRun {
    private static final int LIST_SIZE = 8;

    public static void main1(String[] args) {
        //192.168.50.52
        ArrayList<String> ipList = Lists.newArrayList(ArrayUtil.sub(args, 0, args.length - 1));

        log.info("ip[{}]", StringUtils.join(ipList, ","));
        ArrayList<CompletableFuture> completableFutures = Lists.newArrayList();
        for (String ip : ipList) {
            completableFutures.add(CompletableFuture.runAsync(() -> {
                EvictingQueue<Integer> queue = EvictingQueue.create(LIST_SIZE);
                while (true) {
                    try {
                        ThreadUtil.safeSleep(Integer.valueOf(args[args.length - 1]));
                        byte[] tcpRead = TcpToolUtils.postReAssignByte(ip, 5302, 50);
                        Integer readPound = funParseRead.apply(tcpRead);
                        queue.add(readPound);
                        printInfo(ip, queue);

                    } catch (Exception e) {
                        log.info("读取异常[{}]", ip);
                    }
                }
            }));

        }
        completableFutures.stream().map(CompletableFuture::join).collect(Collectors.toList());

    }

    private static void printInfo(String ip, EvictingQueue<Integer> queue) {
        long sum = queue.stream().mapToLong(item -> item.longValue()).sum();
        String join = StringUtils.join(queue.iterator(), ',');
        if (sum > 0) {
            log.info("当前IP[{}]队列中元素:[{}][{}]", ip, queue.size(), join);
        } else {
            System.out.println(ip + "读取到的数据" + join);
        }

    }


    static FunctionParse<byte[], Integer> funParseRead = (readBytes) -> Optional.ofNullable(readBytes)
            .map(item -> {
                String s = new String(item, StandardCharsets.UTF_8);
                //log.info("地磅读取返回[{}]", s);
                return s;
            }).filter(item -> item.length() > 36).map(item -> {
                List<String> collect = Arrays.stream(item.split("\u0003\u0002"))
                        .filter(str -> StringUtils.length(str.trim()) >= 9)
                        .map(str -> StringUtils.substring(str, 2, str.length() - 3))
                        .collect(Collectors.toList());
                //读取到的不是标准，截取2位的第二位
                String parseIntStr = collect.size() >= 2 ? collect.get(1) : "";
                return Integer.parseInt(parseIntStr);
            }).get();
    static FunctionPound<String, String, Integer, byte[]> funcPound = (ip, port, readLen) -> TcpToolUtils.postReAssignByte(ip, Integer.parseInt(port), readLen);


}
